package com.hw.job_1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentTool {

    // 判断是否包含学生
    public static boolean isExistStudent(List<Student> stArr, Student newSt) {
        if (stArr.isEmpty()) { return false; }
        boolean isExist = false;
        for (Student aSt: stArr) {
            if (aSt.equals(newSt)) {
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    // 将对象集合进行归档
    public static void archiveList(List<Student> stArr) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./Students.txt"));
        oos.writeObject(stArr);
    }

    // 读取文件到集合中
    public static List<Student> unArchiveList() throws Exception {
        List<Student> stArr = new ArrayList<>();
        File f = new File("./Students.txt");
        if (f.exists()) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./Students.txt"));
            Object obj = ois.readObject();
            if (obj instanceof List) {
                stArr = (List<Student>) obj;
            }
        }
        return stArr;
    }
}
